<?php
declare(strict_types = 1);

namespace app\logic;

use app\ArrayToolkit;
use app\logic\BaseLogic;
use think\facade\Db;

class Template extends BaseLogic
{
    public function createTemplate(array $fields)
    {
        $fields = $this->filterTemplateFields($fields);

        if (empty($fields)) {
            return null;
        }

        if (!ArrayToolkit::requireds($fields, ['template_id'])) {
            $this->throwHttpException(500, '缺少必要参数');
        }

        if (!empty($this->getTemplateByTemplateId($fields['template_id']))) {
            $this->throwHttpException(500, '该模版已存在');
        }
        $this->getTemplateModel()->save($fields);
        return $this->getTemplateByTemplateId($fields['template_id']);
    }

    public function updateTemplate($id, array $fields)
    {
        $template = $this->getTemplate($id);
        if (!empty($this->getRedisCache()->get('template:'.$id))) {
            $this->getRedisCache()->delete('template:'.$id);
        }

        if (empty($template)) {
            $this->throwHttpException(500, '数据不存在');
        }
        $fields = $this->filterTemplateFields($fields);

        if (empty($fields)) {
            return null;
        }

        if ($fields['template_id'] != $template['template_id'] && !empty($this->getTemplateByTemplateId($fields['template_id']))) {
            $this->throwHttpException(500, '该模版已存在');
        }

        $this->getTemplateModel()->where('id', $template['id'])->update($fields);
        return $this->getTemplate($id);
    }

    public function deleteTemplate($id)
    {
        $template = $this->getTemplate($id);

        if (empty($template)) {
            $this->throwHttpException(500, '数据不存在');
        }
        if (!empty($this->getRedisCache()->get('template:'.$id))) {
            $this->getRedisCache()->delete('template:'.$id);
        }

        return $this->getTemplateModel()->where('id', $id)->delete();
    }

    public function getTemplate($id)
    {
        if (empty($id)) {
            return [];
        }
        return $this->getTemplateModel()->getById($id);
    }

    public function findTemplateByIds(array $ids)
    {
        if (empty($ids)) {
            return [];
        }
        return $this->getTemplateModel()->findByIds($ids);
    }

    public function getTemplateByTemplateId($templateId)
    {
        if (empty($templateId)) {
            return [];
        }
        return $this->getTemplateModel()->getByTemplateId($templateId);
    }

    public function findAllTemplate()
    {
        return $this->getTemplateModel()->findAllTemplate();
    }

    public function getByFields($field, $value)
    {
        return $this->getTemplateModel()->getByFields($field, $value);
    }

    protected function filterTemplateFields(array $fields)
    {
        $fields = ArrayToolkit::parts($fields, [
            'title',
            'template_id',
            'content',
            'data',
            'created_time',
        ]);

        $fields['data'] = json_encode($fields['data']);
        return $fields;
    }

    public function countTemplate(array $conditions)
    {
        return $this->getTemplateModel()->countTemplate($conditions);
    }

    public function searchTemplates(array $conditions, array $orderBy, $start, $limit)
    {
        return $this->getTemplateModel()->searchTemplates($conditions, $orderBy, $start, $limit);
    }

    /**
     * @return \app\model\Template
     */
    protected function getTemplateModel()
    {
        return $this->getModel('Template');
    }
}
